package com.sample.springbatch.job.sample.listener;

import com.sample.springbatch.job.exception.MySkipException;
import com.sample.springbatch.job.sample.config.RepayOrderConfig;
import com.sample.springbatch.model.RepayOrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.listener.StepListenerSupport;
import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.stereotype.Component;

/**
 * 对作业步骤的监听实现类
 * Created by zwg on 2017/8/24.
 */
@Component
public class RepayOrderStepListener extends StepListenerSupport<RepayOrderInfo, RepayOrderInfo> {

    private static final Logger logger = LoggerFactory.getLogger(RepayOrderStepListener.class);

    @Override
    public void onSkipInRead(Throwable t) {
        if(t instanceof FlatFileParseException){
            FlatFileParseException fpe = (FlatFileParseException) t;
            logger.error("job:{} step:{} reader parse MySkipException. line:{} content:{} MySkipException:{}", RepayOrderConfig.REPAY_ORDER_JOB,RepayOrderConfig.REPAY_ORDER_STEP, fpe.getLineNumber(), fpe.getInput(),fpe.getMessage());
        }else{
            logger.error("job:{} step:{} reader 未知错误.{}",RepayOrderConfig.REPAY_ORDER_JOB,RepayOrderConfig.REPAY_ORDER_STEP,t);
        }

    }

    public void afterRead(RepayOrderInfo item) {
    }

    public void onSkipInProcess(RepayOrderInfo item, Throwable t) {
        if(t instanceof MySkipException){
            logger.error("job:{} step:{} process skip Data. content:{} exception:{}",RepayOrderConfig.REPAY_ORDER_JOB,RepayOrderConfig.REPAY_ORDER_STEP,item,t.getMessage());
        }else{
            logger.error("job:{} step:{} process 未知错误.",RepayOrderConfig.REPAY_ORDER_JOB,RepayOrderConfig.REPAY_ORDER_STEP,t);
        }

    }

    public void onSkipInWrite(RepayOrderInfo item, Throwable t) {
        logger.error("job:{} step:{} writer 未知错误.",RepayOrderConfig.REPAY_ORDER_JOB,RepayOrderConfig.REPAY_ORDER_STEP,t);
    }



}
